

const express = require('express');
const app = express();

const request = express.request;
request.addMessage = function ({ msg, type }) {
    if (!type) type = 'info';
    let session = this.session;
    session.messages = session.messages || [];
    session.messages.push({ msg, type });
}

function messagesMiddleware(req, res, next) {
    res.locals.messages = req.session.messages || [];
    res.locals.removeMessage = function () {
        req.session.messages = [];
    }
    next();
}

module.exports = messagesMiddleware;